﻿using System;
using static System.Console; // static 
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

http://gigi.nullneuron.net/gigilabs/tag/c-6/
https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status&referringTitle=Documentation
http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx?PageIndex=2
http://duoco.de/
namespace CSharp6
{
    public class NewCode
    {
        /// <summary>
        /// avoid magic string, and keep string updated when paramter name change
        /// </summary>
        /// <param name="para1"></param>
        /// <param name="para2"></param>
        public static void DoNameOf(string para1, string para2)
        {
            if (string.IsNullOrEmpty(para1) || string.IsNullOrEmpty(para2))
                throw new ArgumentNullException(string.Join(",", nameof(para2), nameof(para1)), "Method Parameters can not be null");
        }

        /// <summary>
        /// Exception Filter
        /// </summary>
        public static void DoExceptionFilter()
        {
			try
			{
				throw new Exception("My Exception");
			}
			catch (Exception ex)
			when (ex.Message.Equals("My Exception", StringComparison.InvariantCultureIgnoreCase))
			{ WriteLine("log error"); }
			catch (Exception ex) when (ex.InnerException != null) { WriteLine(ex.InnerException); }
			catch (Exception ex) { throw; }

            
        }

        #region USE await in catch and finally block
        private static async Task LongTimeWorkMayThrowException()
        {
            Thread.Sleep(10000);
            throw new TimeoutException("Sorry, opration time out");
        }
        private static async Task LogErrorAsync(Exception ex)
        {
            WriteLine(ex.Message);
        }
        private static async Task<bool> CleanupResource()
        {
            //do sth to clean up resource
            return true;
        }
        public static async void DoTryCatchFinallyForAwaitAsync()
        {
            try
            {
                await LongTimeWorkMayThrowException();
            }
            catch (TimeoutException ex)
            {
                await LogErrorAsync(ex);
            }
            finally
            {
                await CleanupResource();
            }
        }
        #endregion



        public void GenerateTeam()
        {
            Person Brian = new Person("Brian", "O'Rouke", "Mgr");
            Team tablet = new Team("Tablet", Brian);


        }

    }



    public class Team
    {
        public string Name { get; set; }
        public Person Lead { get;}=new Person(); //Getter-only auto-properties == readonly
		public List<Person> TeamMembers { get; set; }=new List<Person>(); // keep json-fileds default value is emptu not null
        public Team(string name, Person lead)
        {
            Name = name;
            Lead = lead;
        }
        public Team(string name, Person lead, List<Person> team) : this(name, lead)
        {
            TeamMembers = team;
        }
    }

    public struct Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Postion { get; set; }
		public string FullName=FirstName+Last

        public Person(string fname, string lname, string pos)
        {
            FirstName = fname;
            LastName = lname;
            Postion = pos;
        }
        public Person() :this("Jason","Sun","Dev") { }

    }
}


